var _sizeController;

function SizeController(controller) {
  this.controller = controller;
  window.onresize = this.onresize;
  // try to avoid scroll bars if possible
  this.screenVerticalOffset = 30;
  this.screenHorizontalOffset = 7;
  // position of the board inside boardMain element
  this.boardMain = $("#boardMain");
  this.boardInfo = $("#boardInfo");
  this.board = $("#board");
  this.zoomControls = $("#zoomControls");
  this.minBoardSize = this.zoomControls.width();
  this.xOffset = 0;
  this.yOffset = this.zoomControls.height();
  this.verticalBoardInfoWidth = this.boardInfo.width();
  this.horizontalBoardInfoHeight = this.boardInfo.height();
  this.footer = $("#footer");
  this.header = $("#header");
  _sizeController = this;
  this.onresize();
}

SizeController.prototype.onresize=function(e) {
  // let's see what it looks like with boardInfo vertical bar to the right of the board
  var y1 = $(window).height() - _sizeController.screenVerticalOffset - _sizeController.yOffset
      - _sizeController.footer.height() - _sizeController.header.height();
  var x1 = $(window).width() - _sizeController.screenHorizontalOffset - _sizeController.xOffset
      - _sizeController.verticalBoardInfoWidth;
  // to fit the square board, pick minimal between width and height
  var sizeWithVerticalBar = (x1 < y1) ? x1 : y1;

  // let's see what it looks like with boardInfo horiontal bar under the board
  var y2 = $(window).height() - _sizeController.screenVerticalOffset - _sizeController.yOffset
      - _sizeController.footer.height() - _sizeController.header.height()
      - _sizeController.horizontalBoardInfoHeight;
  var x2 = $(window).width() - _sizeController.screenHorizontalOffset - _sizeController.xOffset;
  // to fit the square board, pick minimal between width and height
  var sizeWithHorizontalBar = (x2 < y2) ? x2 : y2;

  var isVerticalBar = true;
  var size = sizeWithVerticalBar;
  // if it turns out that with horizontal bar the board size is bigger, choose horizontal bar
  if (sizeWithVerticalBar < sizeWithHorizontalBar) {
  	size = sizeWithHorizontalBar;
    isVerticalBar = false;
  }
  if (size < _sizeController.minBoardSize) {
    size = _sizeController.minBoardSize;
  }
  _sizeController.updateElementSizes(size, isVerticalBar);
}

SizeController.prototype.updateElementSizes = function(size, isVerticalBar) {
  this.boardMain.width(size + this.xOffset);
  this.boardMain.height(size + this.yOffset);
  this.board.width(size);
  this.board.height(size);
  this.zoomControls.css({'margin-left':size-this.zoomControls.width()-2 } );
  if (isVerticalBar) {
    this.boardInfo.removeClass("horizontalBar");
    this.boardInfo.addClass("verticalBar");
    // add 2 to account for the border
    this.boardInfo.css({'margin-left': 2 + size + this.xOffset});
    // vertical bar has the same height as the board
    this.boardInfo.height(size + this.yOffset);
    // fixed width for vertical bar
    this.boardInfo.width(this.verticalBoardInfoWidth);
  } else {
    this.boardInfo.removeClass("verticalBar");
    this.boardInfo.addClass("horizontalBar");
    // horizontal bar is to the left, under the board
    this.boardInfo.css({'margin-left': 0});
    // horizontal bar has the same width as the board
    this.boardInfo.width(size + this.xOffset);
    // fixed height for horizontal bar
    this.boardInfo.height(this.horizontalBoardInfoHeight);
  }
  this.controller.snapView();
}

